import pianoPlayerEvent, { type MusicKey } from '@/event/pianoPlayerEvent'
import audioPlayer from '@/lib/music/audioPlayer'
import { music_black_keys, music_white_keys } from '@/lib/music/musicNote'
type KeyType = 'white' | 'black'

const white_keys = [
  '1',
  '2',
  '3',
  '4',
  '5',
  '6',
  '7',
  '8',
  '9',
  '0',
  'q',
  'w',
  'e',
  'r',
  't',
  'y',
  'u',
  'i',
  'o',
  'p',
  'a',
  's',
  'd',
  'f',
  'g',
  'h',
  'j',
  'k',
  'l',
  'z',
  'x',
  'c',
  'v',
  'b',
  'n',
  'm',
]
const black_keys = [
  '!',
  '@',
  '$',
  '%',
  '^',
  '*',
  '(',
  'Q',
  'W',
  'E',
  'T',
  'Y',
  'I',
  'O',
  'P',
  'S',
  'D',
  'G',
  'H',
  'J',
  'L',
  'Z',
  'C',
  'V',
  'B',
]

export const musicWhiteKey: MusicKey[] = music_white_keys.map((key, index) => {
  return {
    keyboard: white_keys[index],
    music: key,
  }
})
export const musicBlackKey: MusicKey[] = music_black_keys.map((key, index) => {
  return {
    keyboard: black_keys[index],
    music: key,
  }
})

export const getMusicKeyByKeyboard = (keyboard: string): MusicKey | null => {
  const whiteKey = musicWhiteKey.find((key) => key.keyboard === keyboard)
  if (whiteKey) {
    return whiteKey
  }
  const blackKey = musicBlackKey.find((key) => key.keyboard === keyboard)
  if (blackKey) {
    return blackKey
  }
  return null
}
export const getMusicKeyByNote = (keyNote: string): MusicKey | null => {
  const whiteKey = musicWhiteKey.find((key) => key.music === keyNote)
  if (whiteKey) {
    return whiteKey
  }
  const blackKey = musicBlackKey.find((key) => key.music === keyNote)
  if (blackKey) {
    return blackKey
  }
  return null
}

export const getMusicKeyIndex = (
  keyNote: MusicKey
): { index: number; type: KeyType } => {
  let index = musicWhiteKey.findIndex((key) => key.music === keyNote.music)
  if (index !== -1) {
    return {
      index,
      type: 'white',
    }
  }
  index = musicBlackKey.findIndex((key) => key.music === keyNote.music)
  return {
    index,
    type: 'black',
  }
}
export const getMusicIndexByNote = (keyNote: string) => {
  let index = musicWhiteKey.findIndex((key) => key.music === keyNote)
  if (index !== -1) {
    return {
      index,
      type: 'white' as KeyType,
    }
  }
  index = musicBlackKey.findIndex((key) => key.music === keyNote)
  return {
    index,
    type: 'black' as KeyType,
  }
}
export const black_keys_left = [
  1, 2, 4, 5, 6, 8, 9, 11, 12, 13, 15, 16, 18, 19, 20, 22, 23, 25, 26, 27, 29,
  30, 32, 33, 34,
]
export const musicInit = () => {
  audioPlayer.preloading(
    ...music_white_keys.map((key) => `/audio/music/${key.toUpperCase()}.mp3`)
  )
  audioPlayer.preloading(
    ...music_black_keys.map(
      (key) => `/audio/music_black/${key.replace('#', 's')}.mp3`
    )
  )
}
export function playerOnIndex(index: number, white: boolean = true): MusicKey {
  if (white) {
    const key = musicWhiteKey[index]
    playerOnKey(key)
    return key
  } else {
    const key = musicBlackKey[index]
    playerOnKey(key)
    return key
  }
}
export function playerOnKey(musicKey: MusicKey) {
  const key = musicKey.music
  pianoPlayerEvent.emit('noteDown', musicKey)
  if (key.includes('#')) {
    audioPlayer.play({
      src: `/audio/music_black/${key.replace('#', 's')}.mp3`,
    })
  } else {
    audioPlayer.play({
      src: `/audio/music/${key}.mp3`,
    })
  }
}
